// ignore_for_file: library_private_types_in_public_api

import 'package:flutter/material.dart';
import 'package:storage/database.dart';
import 'package:storage/model/bind_good_and_company_model_entity.dart';
import 'package:storage/widget/double_input_formatter.dart';
import 'package:storage/widget/int_input_formatter.dart';

class EveryCompanyCountPage extends StatefulWidget {
  const EveryCompanyCountPage({
    super.key,
    required this.title,
    required this.isWarehouseOut,
    required this.index,
    required this.onSubmitCallBack,
    required this.goodsId,
    required this.companyId,
  });

  final String title;
  final bool isWarehouseOut;
  final int index;
  final Function onSubmitCallBack;
  final int goodsId;
  final int companyId;

  @override
  _EveryCompanyCountPageState createState() => _EveryCompanyCountPageState();
}

class _EveryCompanyCountPageState extends State<EveryCompanyCountPage> {
  final TextEditingController _priceController = TextEditingController();
  final TextEditingController _countController = TextEditingController();

  int? bindId;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      /// 如果是出库 查询对应的价格并填写
      List<Map<String, dynamic>> tempList = await DatabaseHelper()
          .getOfferInfo(goodsId: widget.goodsId, companyId: widget.companyId);
      if (tempList.isNotEmpty) {
        BindGoodAndCompanyModelEntity model =
            BindGoodAndCompanyModelEntity.fromJson(tempList.first);
        bindId = model.id;

        _priceController.text = (model.companyPrice ?? 0.0).toString();
      }
    });
  }

  @override
  void dispose() {
    _countController.dispose();
    _priceController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(8),
      width: widget.isWarehouseOut ? 800 : 600,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(width: 150, child: Text(widget.title)),
          const SizedBox(width: 10),
          SizedBox(
            width: 220,
            child: TextField(
              controller: _priceController,
              inputFormatters: [DoubleInputFormatter()],
              onChanged: (value) {
                if (widget.isWarehouseOut) {
                  widget.onSubmitCallBack.call(
                      widget.index,
                      double.parse(_priceController.text.isEmpty
                          ? '0'
                          : _priceController.text),
                      int.parse(_countController.text.isEmpty
                          ? '0'
                          : _countController.text),
                      bindId);
                } else {
                  widget.onSubmitCallBack.call(
                      widget.index,
                      double.parse(_priceController.text.isEmpty
                          ? '0'
                          : _priceController.text),
                      null,
                      bindId);
                }
              },
              decoration: InputDecoration(
                  border: OutlineInputBorder(
                    borderSide:
                        const BorderSide(width: 2.0, color: Colors.black),
                    // 边框样式
                    borderRadius: BorderRadius.circular(10.0), // 设置圆角半径
                  ),
                  hintText: '填写${widget.title}的报价',
                  hintStyle: const TextStyle(color: Colors.grey)),
            ),
          ),
          if (widget.isWarehouseOut) ...[
            const SizedBox(width: 10),
            SizedBox(
              width: 220,
              child: TextField(
                controller: _countController,
                inputFormatters: [IntegerInputFormatter()],
                onChanged: (value) {
                  if (widget.isWarehouseOut) {
                    widget.onSubmitCallBack.call(
                        widget.index,
                        double.parse(_priceController.text.isEmpty
                            ? '0'
                            : _priceController.text),
                        int.parse(_countController.text.isEmpty
                            ? '0'
                            : _countController.text),
                        bindId);
                  } else {
                    widget.onSubmitCallBack.call(
                        widget.index,
                        double.parse(_priceController.text.isEmpty
                            ? '0'
                            : _priceController.text),
                        null,
                        bindId);
                  }
                },
                decoration: InputDecoration(
                    border: OutlineInputBorder(
                      borderSide:
                          const BorderSide(width: 2.0, color: Colors.black),
                      // 边框样式
                      borderRadius: BorderRadius.circular(10.0), // 设置圆角半径
                    ),
                    hintText: '填写${widget.title}的出库数量',
                    hintStyle: const TextStyle(color: Colors.grey)),
              ),
            ),
          ]
        ],
      ),
    );
  }
}
